package com.geniusk.system.controller;

import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.common.utils.PageUtil;
import com.geniusk.system.model.bo.LoginLog.DeleteLoginLogByIdsBO;
import com.geniusk.system.model.bo.LoginLog.SelectLoginLogByPageBO;
import com.geniusk.system.model.pojo.LoginLog;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.system.service.LoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 登陆日志接口
 */
@RestController
@RequestMapping("/system/loginLog")
public class LoginLogController {

    @Autowired
    private LoginLogService loginLogService;

    /**
     * 登陆日志（根据分页查询）
     */
    @PostMapping("/selectLoginLogByPage")
    @PreFuncAuth("MONITOR:LOGINLOG:SELECT")
    public JSONResult selectLoginLogByPage(@Valid @RequestBody SelectLoginLogByPageBO selectLoginLogByPageBO){
        int pageNum = selectLoginLogByPageBO.getPageNum();
        int pageSize = selectLoginLogByPageBO.getPageSize();
        LoginLog loginLog = JSONUtil.parse(selectLoginLogByPageBO).toBean(LoginLog.class);
        PageUtil.startPage(pageNum,pageSize);
        List<LoginLog> selectScheduleJobLogList = loginLogService.selectLoginLogByPage(loginLog);
        Map<String,Object> result = PageUtil.getResultTableData(selectScheduleJobLogList);
        return JSONResult.ok(result);
    }

    /**
     * 删除登陆日志
     */
    @PostMapping("/deleteLoginLogByIds")
    @PreFuncAuth("MONITOR:LOGINLOG:DELETE")
    public JSONResult deleteLoginLogByIds(@Valid @RequestBody DeleteLoginLogByIdsBO deleteLoginLogByIdsBO) {
        int rows = loginLogService.deleteLoginLogByIds(deleteLoginLogByIdsBO.getLoginLogIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
